package com.ruoyi.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.system.dto.Logg;
import com.ruoyi.system.dto.BusinessinTypeEnum;
import com.ruoyi.system.domain.RR;
import com.ruoyi.system.utils.*;
import com.ruoyi.system.mapper.SysFileMapper;
import com.ruoyi.system.domain.SysinFile;
import com.ruoyi.system.domain.vo.PageFileVo;
import com.ruoyi.system.service.ISysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lgy
 */
@Slf4j
@PreAuthorize("isAuthenticated()")
@RestController
@RequestMapping("system/file")
public class SysFileController extends BaseinController {

    @Autowired
    private SysFileMapper fileMapper;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private ISysFileService sysFileService;


    @PreAuthorize("@ss.hasPermi('system:file:page')")
    @GetMapping("/page")
    public TableinDataInfo findPage(SysinFile sysFile) {
        startPage();
        List<SysinFile> list = sysFileService.selectSysFileList(sysFile);
        return getDataTable(list);
    }

    @PreAuthorize("hasAuthority('system:file:edit')")
    @Logg(title = "文件管理",businessType = BusinessinTypeEnum.UPDATE)
    @PostMapping("/update")
    public RR update(@RequestBody SysinFile files) {
        return RR.ok().setData(fileMapper.updateById(files));
    }

    @PreAuthorize("hasAuthority('system:file:remove')")
    @Logg(title = "文件管理",businessType = BusinessinTypeEnum.DELETE)
    @DeleteMapping()
    public RR delete(@RequestBody SysinFile sysFile) {
        try {
            String fileNanme = SecurityUserUtil.getSecurityUser().getUsername() + sysFile.getUrl().substring(sysFile.getUrl().lastIndexOf("/"));
            minioUtils.deleteObject(sysFile.getFileType(), fileNanme);
        } catch (Exception e) {
            return RR.error("文件删除失败");
        }
        fileMapper.deleteById(sysFile.getId());
        return RR.ok();
    }

    @PreAuthorize("hasAuthority('system:file:remove')")
    @Logg(title = "文件管理",businessType = BusinessinTypeEnum.DELETE)
    @DeleteMapping("/del/batch")
    public RR deleteBatch(@RequestBody List<SysinFile> sysFiles) {
        List<Integer> collect = sysFiles.stream().map(SysinFile::getId).collect(Collectors.toList());
        for (SysinFile sysFile : sysFiles) {
            try {
                String fileNanme = SecurityUserUtil.getSecurityUser().getUsername() + sysFile.getUrl().substring(sysFile.getUrl().lastIndexOf("/"));
                minioUtils.deleteObject(sysFile.getFileType(), fileNanme);
            } catch (Exception e) {
                log.error("文件删除失败{}",e.getMessage());
            }
        }
        fileMapper.deleteBatchIds(collect);
        return RR.ok();
    }


}

